// 节流
export function throttle(func, delay) {
  let lastTime = 0;
  let timer = null;

  return function (...args) {
    const now = Date.now();
    const remaining = delay - (now - lastTime);

    if (remaining <= 0) {
      // 超过节流时间立即执行
      func.apply(this, args);
      lastTime = now;
    } else if (!timer) {
      // 首次未超时设置延迟执行
      timer = setTimeout(() => {
        func.apply(this, args);
        lastTime = Date.now();
        timer = null;
      }, remaining);
    }
  };
}